<template>
  <el-dialog class="change-password" width="600px" title="修改密码" :visible.sync="visible" append-to-body :close-on-click-modal="false" @close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="150px">
      <el-form-item label="原密码" prop="password1">
        <el-input id="iptA2OldPassword" class="w-60percent" v-model="form.password1" type="password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="password2">
        <el-input id="iptA2NewPassword" class="w-60percent" v-model="form.password2" type="password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="password3">
        <el-input id="iptA2ConfirmPassword" class="w-60percent" v-model="form.password3" type="password" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button id="btnA2Cancel" @click="visible = false">取 消</el-button>
      <el-button id="btnA2Submit" type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { reset } from '@/api/login'
import md5 from 'js-md5'
export default {
  data() {
    const validatePassword2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else if (value === this.form.password1) {
        callback(new Error('新密码不能和原密码一样!'))
      } else {
        callback()
      }
    }
    const validatePassword3 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新密码'))
      } else if (value !== this.form.password2) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      jobNumber: '杨帆',
      form: {
        password1: '',
        password2: '',
        password3: ''
      },
      rules: {
        password1: [
          { required: true, message: '请输入原密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        password2: [
          { required: true, validator: validatePassword2, trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        password3: [
          { required: true, validator: validatePassword3, trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.visible = false
          console.log(this.form)
          let param = {
            jobNumber: this.$store.state.Login.loginUser.jobNumber || 'BL000',
            password: md5(this.form.password1),
            newPassword: md5(this.form.password2)
          }
          reset(param)
            .then(res => {
              this.$message.success(res.msg)
              sessionStorage.setItem('loginTimes', '0')
            })
            .catch(err => {
              this.$message.error(err)
            })
        } else {
          return false
        }
      })
    },
    close() {
      this.$refs.form.resetFields()
      this.form.password1 = ''
      this.form.password2 = ''
      this.form.password3 = ''
    },
    open(username) {
      this.username = username
      this.visible = true
      console.log('jinru le')
    }
  }
}
</script>

<style lang="scss" scoped>
.change-password {
  .el-dialog__header {
    line-height: 1;
    .el-dialog__title {
      font-size: 16px;
    }
  }
  .el-dialog__body {
    padding: 20px 80px 20px 40px;
  }
  .el-dialog__footer {
    padding: 0 80px 20px 20px;
  }
}
</style>